# -*- coding:utf-8 -*-
from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView

from Apps.Interface.filters.interfaceEnvFilter import InterfaceEnvFilter
from Apps.Interface.models import InterfaceVariableEnv
from Apps.Interface.ser.interfaceEnv import InterfaceEnvSer, InterfaceEnvNameSer
from Apps.common.response import SuccessResponse
import logging

logger = logging.getLogger("django")


class InterfaceEnvView(ModelViewSet):
    queryset = InterfaceVariableEnv.objects.all()
    serializer_class = InterfaceEnvSer
    filter_class = InterfaceEnvFilter

    # authentication_classes = []

    # 创建和更新接口
    def create(self, request, *args, **kwargs):
        try:
            instance = InterfaceVariableEnv.objects.get(id=request.data["id"])
            serializer = self.get_serializer(instance, data=request.data)
        except Exception as e:
            serializer = self.get_serializer(data=request.data)

        serializer.is_valid(raise_exception=True)
        serializer.save()
        headers = self.get_success_headers(serializer.data)
        return SuccessResponse(serializer.data["id"], headers=headers)


class InterfaceEnvListView(APIView):

    def get(self, request, project_id):
        logger.info(project_id)
        queryset = InterfaceVariableEnv.objects.filter(project_id=project_id).all()
        serializer = InterfaceEnvNameSer(queryset)
        return SuccessResponse(serializer.data)
